package main

import (
	"flag"
	"gateway/dao"
	"gateway/http_proxy_router"
	"gateway/public"
	"gateway/router"
	"os"
	"os/signal"
	"syscall"
)

var (
	env = flag.String("env", "dev", "环境变量")
)

func main() {
	flag.Parse()
	if *env == "dev" {
		public.InitConf(*env)
		public.InitDBPool()
		public.InitRedisPool()
		defer func() {
			public.CloseDB()
			public.CloseRedisConn()
		}()

		// 后台管理服务(http)
		go func() {
			router.ServerRun()
		}()

		dao.ServiceManagerHandler.LoadOnce()
		dao.AppManagerHandler.LoadOnce()

		// 代理服务(http)
		go func() {
			http_proxy_router.HttpServerRun()
		}()
		quit := make(chan os.Signal)
		signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
		<-quit

		// 关闭服务
		router.ServerStop()
		http_proxy_router.HttpServerStop()
	} else {

		public.InitConf(*env)
		public.InitDBPool()
		public.InitRedisPool()
		defer func() {
			public.CloseDB()
			public.CloseRedisConn()
		}()

		// 后台管理服务(https)
		go func() {
			router.ServerRun()
		}()

		dao.ServiceManagerHandler.LoadOnce()
		dao.AppManagerHandler.LoadOnce()

		// 代理服务(https)
		go func() {
			http_proxy_router.HttpsServerRun()
		}()
		quit := make(chan os.Signal)
		signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
		<-quit

		// 关闭服务
		http_proxy_router.HttpsServerStop()
		router.ServerStop()
	}
}
